from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
from PetNetadmin.serializers import OrderSerializer
from NetShop.models import Order
from admincharge.models import SellPet
from admincharge.models import ShopGoods


class getOrder(APIView):
    def get(self,request):
        custom_id = request.GET.get('id')
        status = request.GET.get('status')
        get_all_list = Order.objects.all().filter(custom_id=custom_id).filter(buy_status__contains=status)
        serializer = OrderSerializer(get_all_list,many=True)
        result = {
            'data':serializer.data,
            'code': 2000,
            'msg': '查询成功'
        }
        return Response(result)
    def post(self,request):
        print(request.data['quantity'])
        data = {
            'shop_id':request.data['id'],
            'name':request.data['name'],
            'address':request.data['adpca'],
            'province':request.data['province'],
            'city':request.data['city'],
            'region':request.data['region'],
            'detail':request.data['detail'],
            'mobile':request.data['mobile'],
            'custom_id':request.data['custom_id'],
            'order_id':request.data['orderNumber'],
            'kind':request.data['kind'],
            'shop_name':request.data['shop_name'],
            'picture_url':request.data['picture'],
            'totalprice':request.data['totalprice'],
            'quantity':request.data['quantity'],
            'buy_status':request.data['buy_status']
        }
        print(data)
        if '粮' in data['kind']:
            try:
                # 根据shop_id获取ShopGoods模型实例
                shop_goods = ShopGoods.objects.get(id=data['shop_id'])
                # 检查库存是否足够
                if shop_goods.quantity < int(data['quantity']):
                    shop_goods.status = '3'
                    shop_goods.save()
                    return Response({'msg': '已经没有货了'}, status=status.HTTP_400_BAD_REQUEST)
                    # 减少库存
                shop_goods.quantity -= int(data['quantity'])
                if shop_goods.quantity == 0:
                    shop_goods.status = '3'
                    shop_goods.save()
                shop_goods.save()
            except ShopGoods.DoesNotExist:
                return Response({'msg': '店铺商品不存在'}, status=status.HTTP_400_BAD_REQUEST)
        else:
            try:
                # 根据shop_id获取SellPet模型实例
                sell_pet = SellPet.objects.get(id=data['shop_id'])
                # 检查库存是否足够
                if sell_pet.quantity < int(data['quantity']):
                    sell_pet.status = "3"
                    sell_pet.save()
                    return Response({'msg': '没有足够的货存了'}, status=status.HTTP_400_BAD_REQUEST)
                sell_pet.quantity -= int(data['quantity'])
                if sell_pet.quantity == 0:
                    sell_pet.status = "3"
                    sell_pet.save()
                    # 减少库存
                sell_pet.save()
            except SellPet.DoesNotExist:
                return Response({'msg': '待售宠物不存在'}, status=status.HTTP_400_BAD_REQUEST)
        serializer = OrderSerializer(data=data,partial=True)
        if serializer.is_valid(raise_exception=True):
            serializer.save()

            result = {
                'code': 2000,
                'msg': '建立订单成功'
            }
            return Response(result, status=status.HTTP_200_OK)
        return Response({'msg':'下单失败'},status=status.HTTP_404_NOT_FOUND)

@api_view(['POST'])
def cancel(request):
    print('cancel:id',request.data['id'])
    print('request',request.data)
    id = request.data['id']
    order = Order.objects.get(id=id)
    quantity = order.quantity
    data = {
        'buy_status':'2'
    }
    shop_kind = order.kind
    shop_id = order.shop_id
    if "粮" in shop_kind:
        try:
            # 根据shop_id获取ShopGoods模型实例
            shop_goods = ShopGoods.objects.get(id=shop_id)
            shop_goods.quantity+=quantity
            shop_goods.save()
        except ShopGoods.DoesNotExist:
            return Response({'msg': '物品不存在'}, status=status.HTTP_400_BAD_REQUEST)
    else:
        try:
            sell_pets = SellPet.objects.get(id=shop_id)
            sell_pets.quantity+=quantity
            sell_pets.save()
        except SellPet.DoesNotExist:
            return Response({'msg': '物品不存在'}, status=status.HTTP_400_BAD_REQUEST)
    serializer =OrderSerializer(order,data=data,partial=True)
    if serializer.is_valid(raise_exception=True):
        serializer.save()
        result = {
            'code': 2000,
            'msg': '取消订单成功'
        }
        return Response(result, status=status.HTTP_200_OK)
    return Response({'msg': '取消失败'}, status=status.HTTP_404_NOT_FOUND)
